package top.ajunnihao.core.util.resp;

/**
 * 响应数据体
 */
public class JResp<T> extends BaseResp {

    /**
     * 响应数据
     */
    private T data;

    /**
     * 成功响应码
     */
    private static final Integer OK_CODE = 200;

    /**
     * 失败响应码
     */
    private static final Integer ERROR_CODE = 900;

    private JResp() {

    }

    private JResp(T data) {
        this.data = data;
    }

    private JResp(CodeEnum codeEnum) {
        super(codeEnum);
    }

    private JResp(CodeEnum codeEnum, T data) {
        super(codeEnum);
        this.data = data;
    }

    private JResp(CodeEnum codeEnum, String message, T data) {
        super(codeEnum, message);
        this.data = data;
    }

    private JResp(CodeEnum codeEnum, Integer statusCode, T data) {
        super(codeEnum, statusCode);
        this.data = data;
    }

    private JResp(CodeEnum codeEnum, Integer statusCode, String message, T data) {
        super(codeEnum, statusCode, message);
        this.data = data;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 成功响应
     */
    public static BaseResp success() {
        return new BaseResp(CodeEnum.SUCCESS);
    }

    public static BaseResp success(String message) {
        return new BaseResp(CodeEnum.SUCCESS, message);
    }

    public static BaseResp success(Integer statusCode) {
        return new BaseResp(CodeEnum.SUCCESS, statusCode);
    }

    public static BaseResp success(Integer statusCode, String message) {
        return new BaseResp(CodeEnum.SUCCESS, statusCode, message);
    }

    public static <T> JResp<T> success(T data) {
        return new JResp<T>(CodeEnum.SUCCESS, data);
    }

    public static <T> JResp<T> success(T data, String message) {
        return new JResp<T>(CodeEnum.SUCCESS, message, data);
    }

    public static <T> JResp<T> success(Integer statusCode, T data) {
        return new JResp<T>(CodeEnum.SUCCESS, statusCode, data);
    }

    public static <T> JResp<T> success(Integer statusCode, T data, String message) {
        return new JResp<T>(CodeEnum.SUCCESS, statusCode, message, data);
    }

    /**
     * 失败响应
     */
    public static BaseResp fail() {
        return new BaseResp(CodeEnum.FAIL);
    }

    public static BaseResp fail(String message) {
        return new BaseResp(CodeEnum.FAIL, message);
    }

    public static BaseResp fail(Integer statusCode) {
        return new BaseResp(CodeEnum.FAIL, statusCode);
    }

    public static BaseResp fail(Integer statusCode, String message) {
        return new BaseResp(CodeEnum.FAIL, statusCode, message);
    }

    public static <T> JResp<T> fail(T data) {
        return new JResp<T>(CodeEnum.FAIL, data);
    }

    public static <T> JResp<T> fail(T data, String message) {
        return new JResp<T>(CodeEnum.FAIL, message, data);
    }

    public static <T> JResp<T> fail(Integer statusCode, T data) {
        return new JResp<T>(CodeEnum.FAIL, statusCode, data);
    }

    public static <T> JResp<T> fail(Integer statusCode, T data, String message) {
        return new JResp<T>(CodeEnum.FAIL, statusCode, message, data);
    }

    /**
     * 200 OK 响应
     */
    public static BaseResp ok() {
        return new BaseResp(CodeEnum.SUCCESS, OK_CODE);
    }

    public static BaseResp ok(String message) {
        return new BaseResp(CodeEnum.SUCCESS, OK_CODE, message);
    }

    public static <T> JResp<T> ok(T data) {
        return new JResp<T>(CodeEnum.SUCCESS, OK_CODE, data);
    }

    public static <T> JResp<T> ok(T data, String message) {
        return new JResp<T>(CodeEnum.SUCCESS, OK_CODE, message, data);
    }

    /**
     * 900 error 错误响应
     */
    public static BaseResp error() {
        return new BaseResp(CodeEnum.FAIL, ERROR_CODE);
    }

    public static BaseResp error(String message) {
        return new BaseResp(CodeEnum.FAIL, ERROR_CODE, message);
    }

    public static <T> JResp<T> error(T data) {
        return new JResp<T>(CodeEnum.FAIL, ERROR_CODE, data);
    }

    public static <T> JResp<T> error(T data, String message) {
        return new JResp<T>(CodeEnum.FAIL, ERROR_CODE, message, data);
    }

    @Override
    public String toString() {
        return "JResp{" +
                "data=" + data +
                "} " + super.toString();
    }

}
